file(GLOB_RECURSE CONFLUO_CLIENT_SOURCES src/main/java/*.java)
set(JAVA_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${LIB_INSTALL_DIR}/java")
set(JAVA_DOC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${DOC_INSTALL_DIR}/java")

set(CONFLUO_CLIENT_ARTIFACTS confluo-client-${CONFLUO_VERSION}.jar confluo-client-${CONFLUO_VERSION}.pom)
set(SERVER_EXEC "${CMAKE_CURRENT_BINARY_DIR}/../librpc/confluod")

set(BUILD_XML_IN "${CMAKE_CURRENT_SOURCE_DIR}/build.xml.in")
set(BUILD_XML "${CMAKE_CURRENT_BINARY_DIR}/build.xml")
configure_file(${BUILD_XML_IN} ${BUILD_XML} @ONLY)

set(JAVA_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/main/java)
set(JAVA_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/test/java)
file(GLOB JAVA_SOURCES ${JAVA_SRC_DIR}/*.java)
file(GLOB JAVA_TESTS ${JAVA_TEST_DIR}/*.java)
add_custom_command(
    OUTPUT ${CONFLUO_CLIENT_ARTIFACTS}
    DEPENDS ${JAVA_SOURCES} ${JAVA_TESTS}
    COMMAND ${Ant_EXECUTABLE} ${ANT_FLAGS} -Dbuild.dir="${CMAKE_CURRENT_BINARY_DIR}" -f ${BUILD_XML} jar
    MAIN_DEPENDENCY ${BUILD_XML}
    DEPENDS ${CONFLUO_CLIENT_SOURCES}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

add_custom_target(javaclient ALL
    COMMENT "Building Confluo Java Client library using Ant"
    DEPENDS ${CONFLUO_CLIENT_ARTIFACTS}
)

if (BUILD_TESTS)
  add_test(NAME JavaClientTest
    COMMAND ${Ant_EXECUTABLE} ${ANT_FLAGS} -Dbuild.dir="${CMAKE_CURRENT_BINARY_DIR}" -f ${BUILD_XML} test
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  )
endif()
